<?php

class Model_tfidf extends CI_Model {
	
	public function df($hasil){
		if($hasil){
		foreach($hasil['no'] as $no_jawaban){
			foreach($hasil['jawaban'][$no_jawaban]['S1'] as $no_token){
				$df[$no_jawaban][$no_token] = 0;
				
				if($hasil['jawaban'][$no_jawaban]['hasil'][$no_token]>0){
					$df[$no_jawaban][$no_token] = $df[$no_jawaban][$no_token] + 1;
				}
				else{
					$df[$no_jawaban][$no_token] = $df[$no_jawaban][$no_token] + 0;
				}
				
				foreach($hasil['no_kunci'] as $no_kunci){
					if($hasil['kunci'][$no_jawaban][$no_kunci]['hasil'][$no_token]>0){
						$df[$no_jawaban][$no_token] = $df[$no_jawaban][$no_token] + 1;
					}
					else{
						$df[$no_jawaban][$no_token] = $df[$no_jawaban][$no_token] + 0;
					}
				}
			}
		}
		return $df;
		}
	}
	
	public function Ddf($hasil, $jumlah_kunci){
		if($hasil){
		foreach($hasil['no'] as $no_jawaban){
			foreach($hasil['jawaban'][$no_jawaban]['S1'] as $no_token){
				if($hasil['df'][$no_jawaban][$no_token]!=0){
					$Ddf['D/df'][$no_jawaban][$no_token] 	= round($jumlah_kunci / $hasil['df'][$no_jawaban][$no_token],$hasil['error_digit']);
					$Ddf['log'][$no_jawaban][$no_token] 	= round(log10($Ddf['D/df'][$no_jawaban][$no_token]),$hasil['error_digit']);
				}
				else{
					$Ddf['D/df'][$no_jawaban][$no_token] 	= 0;
					$Ddf['log'][$no_jawaban][$no_token] 	= 0;
				}
			}
		}
		return $Ddf;
		}
	}
	
	public function W($hasil){
		if($hasil){
		foreach($hasil['no'] as $no_jawaban){
			foreach($hasil['jawaban'][$no_jawaban]['S1'] as $no_token){
				$W['jawaban'][$no_jawaban][$no_token] = round($hasil['D/df']['log'][$no_jawaban][$no_token] * $hasil['jawaban'][$no_jawaban]['hasil'][$no_token],$hasil['error_digit']);
				foreach($hasil['no_kunci'] as $no_kunci){
					$W['kunci'][$no_jawaban][$no_kunci][$no_token] = round($hasil['D/df']['log'][$no_jawaban][$no_token] * $hasil['kunci'][$no_jawaban][$no_kunci]['hasil'][$no_token],$hasil['error_digit']);
				}
			}
		}
		return $W;
		}
	}
	
	public function bobot_W($hasil){
		if($hasil){
		foreach($hasil['no'] as $no_jawaban){
			foreach($hasil['no_kunci'] as $no_kunci){
				$hasil[$no_jawaban][$no_kunci] = 0;
				foreach($hasil['jawaban'][$no_jawaban]['S1'] as $no_token){
					if($hasil['jawaban'][$no_jawaban]['hasil'][$no_token] > 0){
						$hasil[$no_jawaban][$no_kunci] = round($hasil[$no_jawaban][$no_kunci] + $hasil['W']['kunci'][$no_jawaban][$no_kunci][$no_token],$hasil['error_digit']);
					}
				}
			}
		}
		return $hasil;
		}
	}
	
	public function VSM($hasil){
		if($hasil){
		foreach($hasil['no'] as $no_jawaban){
			foreach($hasil['jawaban'][$no_jawaban]['S1'] as $no_token){
				$VSM['jawaban'][$no_jawaban][$no_token] = round($hasil['W']['jawaban'][$no_jawaban][$no_token] * $hasil['W']['jawaban'][$no_jawaban][$no_token],$hasil['error_digit']);
				foreach($hasil['no_kunci'] as $no_kunci){
					$VSM['kunci'][$no_jawaban][$no_kunci][$no_token] = round($hasil['W']['kunci'][$no_jawaban][$no_kunci][$no_token] * $hasil['W']['kunci'][$no_jawaban][$no_kunci][$no_token],$hasil['error_digit']);
				}
			}
		}
		return $VSM;
		}
	}
	
	public function kkdoc($hasil){
		if($hasil){
		foreach($hasil['no'] as $no_jawaban){
			foreach($hasil['jawaban'][$no_jawaban]['S1'] as $no_token){
				foreach($hasil['no_kunci'] as $no_kunci){
					$kkdoc[$no_jawaban][$no_kunci][$no_token] = round($hasil['W']['jawaban'][$no_jawaban][$no_token] * $hasil['W']['kunci'][$no_jawaban][$no_kunci][$no_token],$hasil['error_digit']);
				}
			}
		}
		return $kkdoc;
		}
	}
	
	public function sqrt($hasil){
		if($hasil){
		foreach($hasil['no'] as $no_jawaban){
			foreach($hasil['no_kunci'] as $no_kunci){
				$sqrt['jawaban'][$no_jawaban] = 0;
				$sqrt['kkdoc'][$no_jawaban][$no_kunci] = 0;
				$sqrt['kuadrat'][$no_jawaban][$no_kunci] = 0;
				foreach($hasil['jawaban'][$no_jawaban]['S1'] as $no_token){
					$sqrt['kuadrat'][$no_jawaban][$no_kunci] 	= $sqrt['kuadrat'][$no_jawaban][$no_kunci] + $hasil['VSM']['kunci'][$no_jawaban][$no_kunci][$no_token];
					$sqrt['jawaban'][$no_jawaban] 				= $sqrt['jawaban'][$no_jawaban] + $hasil['VSM']['jawaban'][$no_jawaban][$no_token];
					if($hasil['jawaban'][$no_jawaban]['hasil'][$no_token] > 0){
						$sqrt['kkdoc'][$no_jawaban][$no_kunci] 	= $sqrt['kkdoc'][$no_jawaban][$no_kunci] + $hasil['kkdoc'][$no_jawaban][$no_kunci][$no_token];
					}
				}
				$sqrt['kuadrat'][$no_jawaban][$no_kunci] 	= round(sqrt($sqrt['kuadrat'][$no_jawaban][$no_kunci]),$hasil['error_digit']);
				$sqrt['jawaban_2'][$no_jawaban] 			= round(sqrt($sqrt['jawaban'][$no_jawaban]),$hasil['error_digit']);
			}
		}
		return $sqrt;
		}
	}
	
	public function cosin($hasil){
		if($hasil){
		foreach($hasil['no'] as $no_jawaban){
			if($hasil['sqrt']['jawaban'][$no_jawaban]!=0){
				$cosin['jawaban'][$no_jawaban] = round(($hasil['sqrt']['jawaban'][$no_jawaban]/($hasil['sqrt']['jawaban_2'][$no_jawaban] * $hasil['sqrt']['jawaban_2'][$no_jawaban])),$hasil['error_digit']);
			}
			else{
				$cosin['jawaban'][$no_jawaban] = 0;
			}
			foreach($hasil['no_kunci'] as $no_kunci){
				if($hasil['sqrt']['kkdoc'][$no_jawaban][$no_kunci]!=0){
					$cosin[$no_jawaban][$no_kunci] = round(($hasil['sqrt']['kkdoc'][$no_jawaban][$no_kunci])/($hasil['sqrt']['jawaban_2'][$no_jawaban] * $hasil['sqrt']['kuadrat'][$no_jawaban][$no_kunci]),$hasil['error_digit']);
				}
				else{
					$cosin[$no_jawaban][$no_kunci] = 0;
				}
			}
		}
		return $cosin;
		}
	}
	
}
